package info.wegosoft.android.qauth.tencent;

import info.wegosoft.android.qauth.tencent.QWeiboType.PageFlag;
import info.wegosoft.android.qauth.tencent.QWeiboType.ResultType;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


public class QWeiboSyncApi {

	/**
	 * Get request token.
	 * 
	 * @param customKey
	 *            Your AppKey.
	 * @param customSecret
	 *            Your AppSecret.
	 * @return The request token.
	 */
	public String getRequestToken(String customKey, String customSecret) {
		String url = "https://open.t.qq.com/cgi-bin/request_token";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		//The OAuth Call back URL(You should encode this url if it
		//contains some unreserved characters).
		oauthKey.callbackUrl = "null";//"http://www.qq.com";

		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * Get access token.
	 * 
	 * @param customKey
	 *            Your AppKey.
	 * @param customSecret
	 *            Your AppSecret
	 * @param requestToken
	 *            The request token.
	 * @param requestTokenSecret
	 *            The request token Secret
	 * @param verify
	 *            The verification code.
	 * @return
	 */
	public String getAccessToken(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect, String verify) {

		String url = "https://open.t.qq.com/cgi-bin/access_token";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;
		oauthKey.verify = verify;

		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 获取自己的资料.
	 * 
	 * @param customKey
	 *            Your AppKey
	 * @param customSecret
	 *            Your AppSecret
	 * @param requestToken
	 *            The access token
	 * @param requestTokenSecret
	 *            The access token secret
	 * @param format
	 *            Response format, xml or json
	 * @return Response messages based on the specified format.
	 */
	public String getSelfInfo(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect, ResultType format) {

		String url = "http://open.t.qq.com/api/user/info";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}

		parameters.add(new QParameter("format", strFormat));
		
		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * Get home page messages.
	 * 
	 * @param customKey
	 *            Your AppKey
	 * @param customSecret
	 *            Your AppSecret
	 * @param requestToken
	 *            The access token
	 * @param requestTokenSecret
	 *            The access token secret
	 * @param format
	 *            Response format, xml or json
	 * @param pageFlag
	 *            Page number.
	 * @param nReqNum
	 *            Number of messages you want.
	 * @return Response messages based on the specified format.
	 */
	public String getHomeMsg(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect, ResultType format,
			PageFlag pageFlag, int nReqNum, String lastPageTimeStamp) {

		String url = "http://open.t.qq.com/api/statuses/home_timeline";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}

		parameters.add(new QParameter("format", strFormat));
		parameters.add(new QParameter("pageflag", String.valueOf(pageFlag
				.ordinal())));
		parameters.add(new QParameter("reqnum", String.valueOf(nReqNum)));

		if(lastPageTimeStamp!=null){
			
			parameters.add(new QParameter("pagetime", lastPageTimeStamp));
			
		}
		
		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * Publish a Weibo message.
	 * 
	 * @param customKey
	 *            Your AppKey
	 * @param customSecret
	 *            Your AppSecret
	 * @param requestToken
	 *            The access token
	 * @param requestTokenSecrect
	 *            The access token secret
	 * @param content
	 *            The content of your message
	 * @param pic
	 *            The files of your images.
	 * @param format
	 *            Response format, xml or json(Default).
	 * @return Result info based on the specified format.
	 */
	public String publishMsg(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect, String content,
			String pic, ResultType format) {

		List<QParameter> files = new ArrayList<QParameter>();
		String url = null;
		String httpMethod = "POST";

		if (pic == null || pic.trim().equals("")) {
			url = "http://open.t.qq.com/api/t/add";
		} else {
			url = "http://open.t.qq.com/api/t/add_pic";
			files.add(new QParameter("pic", pic));
		}

		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		List<QParameter> parameters = new ArrayList<QParameter>();

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}

		parameters.add(new QParameter("format", strFormat));
		try {
			parameters.add(new QParameter("content", new String(content
					.getBytes("UTF-8"))));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			return "";
		}
		parameters.add(new QParameter("clientip", "127.0.0.1"));

		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, httpMethod, oauthKey, parameters,
					files);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * 
	 * @param customKey
	 *            Your AppKey
	 * @param customSecret
	 *            Your AppSecret
	 * @param requestToken
	 *            The access token
	 * @param requestTokenSecrect
	 *            The access token secret
	 * @param content
	 *            The content of your message
	 * @param format
	 *            Response format, xml or json(Default).
	 * @param reid
	 * 			  The id of message you want to repost
	 * @return Result info based on the specified format.
	 */
	public String repostMsg(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect, String content,
			ResultType format, String reid) {
		List<QParameter> files = new ArrayList<QParameter>();
		String url = "http://open.t.qq.com/api/t/re_add";
		String httpMethod = "POST";

		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		List<QParameter> parameters = new ArrayList<QParameter>();

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}

		parameters.add(new QParameter("format", strFormat));
		try {
			parameters.add(new QParameter("content", new String(content
					.getBytes("UTF-8"))));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			return "";
		}
		parameters.add(new QParameter("clientip", "127.0.0.1"));

		parameters.add(new QParameter("reid", reid));
		
		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, httpMethod, oauthKey, parameters,
					files);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * 
	 * @param customKey
	 *            Your AppKey
	 * @param customSecret
	 *            Your AppSecret
	 * @param requestToken
	 *            The access token
	 * @param requestTokenSecrect
	 *            The access token secret
	 * @param content
	 *            The content of your message
	 * @param format
	 *            Response format, xml or json(Default).
	 * @param reid
	 * 			  The id of message you want to comment
	 * @return Result info based on the specified format.
	 */
	public String commentMsg(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect, String content,
			ResultType format, String reid) {
		List<QParameter> files = new ArrayList<QParameter>();
		String url = "http://open.t.qq.com/api/t/comment";
		String httpMethod = "POST";

		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		List<QParameter> parameters = new ArrayList<QParameter>();

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}

		parameters.add(new QParameter("format", strFormat));
		try {
			parameters.add(new QParameter("content", new String(content
					.getBytes("UTF-8"))));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			return "";
		}
		parameters.add(new QParameter("clientip", "127.0.0.1"));

		parameters.add(new QParameter("reid", reid));
		
		QWeiboRequest request = new QWeiboRequest();
		String res = null;
		try {
			res = request.syncRequest(url, httpMethod, oauthKey, parameters,
					files);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * 详细说明见http://open.t.qq.com/resource.php?i=1,1
	 * @param customKey
	 * @param customSecret
	 * @param requestToken
	 * @param requestTokenSecrect
	 * @param format 
	 * @param root_id 转发或者回复根结点ID（源微博ID）
	 * @param twitterId 第一页 时填0,继续向下翻页，填上一次请求返回的最后一条记录ID，翻页用
	 * @param pagetime 本页起始时间（第一页 时填0，继续向下翻页：填上一次请求返回的最后一条记录时间）
	 * @return
	 */
	public String getCommentList(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect,
			ResultType format, String root_id,String twitterId,String pagetime) {
		
		List<QParameter> files = new ArrayList<QParameter>();
		String url = "http://open.t.qq.com/api/t/re_list";
		String httpMethod = "GET";

		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		List<QParameter> parameters = new ArrayList<QParameter>();

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}
		
		parameters.add(new QParameter("format", strFormat));
		
		//Flag:标识0 转播列表，1点评列表 2 点评与转播列表
		parameters.add(new QParameter("flag", "1"));

		parameters.add(new QParameter("rootid", root_id));
		
		parameters.add(new QParameter("reqnum", 20 + ""));
		
		parameters.add(new QParameter("twitterid", twitterId));
		
		//PageFlag：（根据dwTime），0：第一页，1：向下翻页，2向上翻页；
		if(twitterId.equals("0")){
			
			parameters.add(new QParameter("pageflag", "0"));
			
		}else{
			
			parameters.add(new QParameter("pagetime", pagetime));
			
			parameters.add(new QParameter("pageflag", "1"));
			
		}
		
		QWeiboRequest request = new QWeiboRequest();
		
		String res = null;
		
		try {
			
			res = request.syncRequest(url, httpMethod, oauthKey, parameters,
					files);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return res;
	}
	
	/**
	 * 返回指定微博的转发和评论数
	 * @param customKey
	 * @param customSecret
	 * @param requestToken
	 * @param requestTokenSecrect
	 * @param format
	 * @param id
	 * @return
	 */
	public String getCountByID(String customKey, String customSecret,
			String requestToken, String requestTokenSecrect,
			ResultType format, String id) {
		
		List<QParameter> files = new ArrayList<QParameter>();
		String url = "http://open.t.qq.com/api/t/re_count";
		String httpMethod = "GET";

		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = customKey;
		oauthKey.customSecrect = customSecret;
		oauthKey.tokenKey = requestToken;
		oauthKey.tokenSecrect = requestTokenSecrect;

		List<QParameter> parameters = new ArrayList<QParameter>();

		String strFormat = null;
		if (format == ResultType.ResultType_Xml) {
			strFormat = "xml";
		} else if (format == ResultType.ResultType_Json) {
			strFormat = "json";
		} else {
			return "";
		}
		
		parameters.add(new QParameter("format", strFormat));
		
		parameters.add(new QParameter("ids", id));
		
		//Flag:0 获取转发计数，1点评计数 2 两者都取
		parameters.add(new QParameter("flag", "2"));
		
		QWeiboRequest request = new QWeiboRequest();
		
		String res = null;
		
		try {
			
			res = request.syncRequest(url, httpMethod, oauthKey, parameters,
					files);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		return res;
	}
}
